package com.yi.wood.web.ctrl;

import com.yi.wood.entity.AppUser;
import com.yi.wood.service.po.AppUserCachePo;
import com.yi.wood.web.ctrl.dto.AppUserParam;
import com.yi.wood.web.service.AppUserProService;
import com.yi.wood.web.util.WoodBeanUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

/**
 * @author MoYi
 * create date 2019/4/14 10:31
 */
@Validated
@RestController
@RequestMapping("/api/au")
@Log4j2
public class AppUserCtrl extends BaseCtrl{

    @Resource
    private AppUserProService appUserProService;

    /**
     * APP用户登录
     * @param param 用户名和密码
     * @param code 验证码
     * @return 用户登录状态和信息
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Object appUserLogin(@RequestBody @Valid AppUserParam param, @RequestParam String code){
        AppUserCachePo appUserCachePo = appUserProService.appUserLogin(param.getUserName(),param.getPassword(), code);
        return success(appUserCachePo);
    }

    /**
     * 获取用户缓存信息
     * @return 用户信息
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public Object getUserInfo(){
        return success(getUserByToken());
    }

    /**
     * 注册新的APP用户
     * @param param 注册时用户信息
     * @return 注册结果
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addAppUser(@RequestBody @Valid AppUserParam param){
        AppUser appUser = new AppUser();
        BeanUtils.copyProperties(param, appUser, WoodBeanUtils.getNullPropertyNames(param));
        appUserProService.addData(appUser);
        return success();
    }

    /**
     * 更新APP用户信息
     * @param param 用户信息 注意ID为必填参数
     * @return 更新结果
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateAppUser(@RequestBody @Valid AppUserParam param){
        AppUser appUser = new AppUser();
        BeanUtils.copyProperties(param, appUser, WoodBeanUtils.getNullPropertyNames(param));
        appUserProService.updateData(appUser);
        return success();
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public Object pageAppUserList(@RequestParam(required = false) String userName,
                                  @RequestParam(required = false) String phone,
                                  @RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize){
        return success(appUserProService.pageAppUserList(userName, phone, pageNum, pageSize));
    }

    /**
     * 查询某个用户的关系网
     * @return 某个用户的上下级关系
     */
    @RequestMapping(value = "/relationship", method = RequestMethod.GET)
    public Object checkUserRelationship(@RequestParam Long userId){
        Map<Object, Object> map = result();
        map.put("parentUser", appUserProService.getUserParentUserInfo(userId));
        map.put("nextNodeUser", appUserProService.getUserNextNodeUserInfo(userId));
        return success(map);
    }
}
